<?php

namespace app\admin\model\tongcheng;

use addons\tongcheng\model\BaseModel;


class ZoneBalanceLogModel extends BaseModel
{
    // 表名
    protected $name = 'tongcheng_zone_balance_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'kind_text',
        'changeable_url'
    ];

    public function getChangeableUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['changeable_type']) ? $data['changeable_type'] : '');
        $list  = [
            DeliveryOrderModel::class                         => 'tongcheng/delivery_order/index',
            \addons\tongcheng\model\DeliveryOrderModel::class => 'tongcheng/delivery_order/index',
            RiderWithdrawModel::class                         => 'tongcheng/rider_withdraw/index',
            \addons\tongcheng\model\RiderWithdrawModel::class => 'tongcheng/rider_withdraw/index',
        ];
        return isset($list[$value]) ? url($list[$value], ['id' => $data['changeable_id']]) : '';
    }

    public function getTypeList()
    {
        return ['INC' => __('Inc'), 'DEC' => __('Dec')];
    }

    public function getKindList()
    {
        return ['DELIVERY' => __('Delivery'), 'WITHDRAW' => __('Withdraw'), 'WX_FEE_INC' => __('Wx_fee_inc'), 'WX_FEE_DEC' => __('Wx_fee_dec')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list  = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getKindTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['kind']) ? $data['kind'] : '');
        $list  = $this->getKindList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function zone()
    {
        return $this->belongsTo('Zone', 'zone_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function rider()
    {
        return $this->belongsTo('Rider', 'rider_id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 多态关联变动对象
     * @return \think\model\relation\MorphTo
     */
    public function changeable()
    {
        return $this->morphTo();
    }
}
